/*
 * Author:  Natalie Schneider
 * Company:  1186 Entertainment
 * Date Modified:  3/25/07
 * Filename:  ClientThread.java
 */

import java.util.*;
import java.io.*;
import java.net.*;
import java.lang.*;

//ClientThread class
public class ClientThread extends Thread
{
	public BufferedReader fromClient;	//output stream from client
	public PrintWriter toClient;        //input stream to client
	private Room currRoom;      			//current room status which Game Lobby or Room user is in
	private String username;   			//user's unique username or temporarily unique guest name (Guest##)

	public boolean isGuest;			//whether client is guest

	private boolean running = true;  	//for thread
	public Socket client;        		//client
	private Server serverApp = null;   	// backref to server

	//constructor
	public ClientThread(Server server1, Socket c1)
	{
		serverApp = server1;   	//set server
		client = c1;    		//set client
		username = "";   		//set username to blank
		currRoom = null;   		//set initial room status to null
		isGuest = false;

		try
	   	{

			//set input and output streams of clients
			fromClient = new BufferedReader(new InputStreamReader(client.getInputStream()));
			toClient = new PrintWriter(new BufferedWriter(new OutputStreamWriter(client.getOutputStream())), true);

			//send any initialization messages

			//start this client thread
			Thread outputThread = new Thread(this);  // Thread is to receive strings
			//outputThread.setPriority(2);
			outputThread.start();                    // from Server

		}
		catch(Exception e)
		{
			System.out.println("Problem initializing client thread: " + e);
		}


	}//end constructor



	//run function of client thread
	public void run()
	{
		String inMsg;

		try
		{
			//runs until thread dies
			while(running == true)
			{
				//receive messages from client and process accordingly
				inMsg = receive();

				if(inMsg.length() > 0)
				{
					serverApp.processMessage(inMsg, this);  //process the message
				}

				//sleep thread for 100ms
				Thread.sleep(100);
			}
		}
		catch(Exception e)
		{
			System.out.println("Error with thread: " + e);
		}
	}//end run

	//send messages to client
	public void send(String outMsg)
	{
		//send message to client
	   	try
	   	{
			System.out.println("Server attempting to send: " + outMsg);
			toClient.println(outMsg);
			System.out.println("Server sent: " + outMsg);
		}
		catch(Exception e)
		{
			System.out.println("Problem sending message to client: " + e);
		}
	}

	//receive messages from client
	public String receive()
	{
	   //receive message from client
		String msg = "";
		try
		{
			msg = fromClient.readLine();
			System.out.println("Server received: " + msg);
		}
		catch(Exception e)
		{
			System.out.println("Problem receiving message from client: " + e);
			System.out.println("Removing client from server");
			serverApp.removeClosedClient(this);
			msg = "";

		}
		return msg;

	}

	//returns username of client
	public void setUsername(String uname1)
	{
		username = uname1;
	}

	//returns int id of current room
	public void setCurrentRoom(Room rm1)
	{
		currRoom = rm1;
	}

	//returns username of client
	public String getUsername()
	{
		return username;
	}

	//returns int id of current room
	public Room getCurrentRoom()
	{
		return currRoom;
	}


	public void leaveRoom()
	{
		//remove this client from the room it's in (if it is in one)
		if(currRoom != null)
		{
			currRoom.roomies.remove(this);
			//update all the users in this room
			serverApp.sendUpdatedUsersToRoomies(currRoom);
		}
		currRoom = null;
	}

	public void enterRoom(Room rm1)
	{
		//remove this client from the room it's in (if it is in one)
		leaveRoom();
		//add to new room's roomies
		rm1.roomies.add(this);
		//set this as current room
		currRoom = rm1;
		//update all the users in this room
		serverApp.sendUpdatedUsersToRoomies(currRoom);
	}




	/** Closes the ClientThread */
	public void close()
	{
		try
		{
			System.out.println("Closing the ClientThread.");

			running = false;
			fromClient.close();
			toClient.close();
			client.close();
		}
		catch (Exception e)
		{
			System.out.println("Error while closing the ClientThread: " + e);
		}
	}


}//end ClientThread class


